package com.tansun.easycare.rule.bakproject.service.impl;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.tansun.easycare.rule.bakproject.dao.RuleProjectResDao;
import com.tansun.easycare.rule.bakproject.entity.RuleProjectRes;
import com.tansun.easycare.rule.bakproject.model.RuleProjectResInfo;
import com.tansun.easycare.rule.bakproject.service.RuleProjectResService;
import com.tansun.easycare.rule.common.service.impl.RuleBaseServiceImpl;
import com.tansun.easycare.rule.exception.BizException;

@Service
public class RuleProjectResServiceImpl extends RuleBaseServiceImpl<RuleProjectResDao, RuleProjectRes> implements RuleProjectResService {

	
	public void deleteByProjectCode(String projectCode) {
		Map<String,String>  map   = new  HashMap<>();
		map.put("projectCode", projectCode);
		this.dao.deleteByProjectCode(map);

	}

	@Override
	public void deleteProjectCopy(String projectCode) {
		RuleProjectResInfo   projectResInfo  = new  RuleProjectResInfo();
		projectResInfo.setProjectCode(projectCode);
		this.dao.deleteProjectCopy(projectResInfo);
		
	}
	
	@Override
	public  void  projectCopy(String  projectCode,String  newProjectName) {
		RuleProjectResInfo  projectInfo   = new  RuleProjectResInfo();
		projectInfo.setProjectCode(projectCode);
		projectInfo.setProjectName(newProjectName);
		

		try {
		    Class<?>  clazz =	this.dao.getClass();
		    Method[] methods = clazz.getMethods();
		    for (Method meth :methods) {
		        String    methName =	meth.getName();
		        Class<?>[]  paramClazz =  meth.getParameterTypes();
		        if (paramClazz != null && paramClazz.length>0) {
		        	Class<?>  pClzz = paramClazz[0];
		        	if (pClzz.isAssignableFrom(RuleProjectResInfo.class) && methName.startsWith("copy")) {
		        		
		        		meth.invoke(this.dao, projectInfo);
		        	}
		        }
		    	
		    }
		} catch (Exception e) {
		
			e.printStackTrace();
			throw  new  BizException("", e.getMessage());
		}
		
	}

}
